package com.queclink.ycm.entity.data.useless;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.entity.data.Data;

import java.text.DecimalFormat;
import java.util.Map;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 19:13:00
 */
public class Data258 extends com.queclink.ycm.entity.data.Data {

    private String VehicleModel;
    private String Data;

    public Data258() {
    }

    @JSONField(
            name = "VehicleModel"
    )
    public String getVehicleModel(JSONObject jsonObject) {
        StringBuffer sb = new StringBuffer();
        Integer satelliteCount = jsonObject.getInteger("VehicleModel");
        String s = HexUtils.intToHex(satelliteCount, 4);
        sb.append(s);
        return sb.toString();
    }

    public void setVehicleModel(String vehicleModel) {
        this.VehicleModel = vehicleModel;
    }

    public String getDataID() {
        // data258
        return "8102";
    }

    public String getLength() {
        return "1B";
    }

    @JSONField(
            name = "Data"
    )
    public String getData(JSONObject jsonObject) {
        StringBuffer sb = new StringBuffer();
        JSONObject data = jsonObject.getJSONObject("Data");
        // 发动机转速
        Integer engineRPM = data.getInteger("EngineRPM");
        String engineRPMStr = HexUtils.intToHex(engineRPM, 4);
        sb.append(engineRPMStr);
        // 速度
        Double speed = data.getDouble("Speed");
        String format1 = new DecimalFormat("0.0").format(speed);
        String replace1 = format1.replace(".", "");
        String speedStr = HexUtils.LongStrToHex(replace1, 4);
        sb.append(speedStr);
        // 发动机冷却液温度
        Double engineCoolantTemperature = data.getDouble("EngineCoolantTemperature");
        String format2 = new DecimalFormat("0.0").format(engineCoolantTemperature);
        String replace2 = format2.replace(".", "");
        String engineCoolantTemperatureStr = HexUtils.LongStrToHex(replace2, 4);
        sb.append(engineCoolantTemperatureStr);
        //
        Integer gear = data.getInteger("Gear");
        String gearStr = HexUtils.intToHex(gear, 2);
        sb.append(gearStr);
        //
        Integer fuelLevel = data.getInteger("FuelLevel");
        String fuelLevelStr = HexUtils.intToHex(fuelLevel, 2);
        sb.append(fuelLevelStr);
        //
        Integer highBeam = data.getInteger("HighBeam");
        String highBeamStr = HexUtils.intToHex(highBeam, 2);
        sb.append(highBeamStr);
        //
        Integer lightSignal = data.getInteger("LightSignal");
        String lightSignalStr = HexUtils.intToHex(lightSignal, 2);
        sb.append(lightSignalStr);
        //
        Integer rightSignal = data.getInteger("RightSignal");
        String rightSignalStr = HexUtils.intToHex(rightSignal, 2);
        sb.append(rightSignalStr);
        //
        Integer oilPressureIndicator = data.getInteger("OilPressureIndicator");
        String oilPressureIndicatorStr = HexUtils.intToHex(oilPressureIndicator, 2);
        sb.append(oilPressureIndicatorStr);
        //
        Integer absIndicator = data.getInteger("ABSIndicator");
        String absIndicatorStr = HexUtils.intToHex(absIndicator, 2);
        sb.append(absIndicatorStr);
        //
        Integer obdIndicator = data.getInteger("OBDIndicator");
        String obdIndicatorStr = HexUtils.intToHex(obdIndicator, 2);
        sb.append(obdIndicatorStr);
        //
        Integer klineAndWaterTemperature = data.getInteger("KlineAndWaterTemperature");
        String klineAndWaterTemperatureStr = HexUtils.intToHex(klineAndWaterTemperature, 2);
        sb.append(klineAndWaterTemperatureStr);
        //
        Integer currentMileage = data.getInteger("CurrentMileage");
        String currentMileageStr = HexUtils.intToHex(currentMileage, 8);
        sb.append(currentMileageStr);
        //
        Integer totalMileage = data.getInteger("TotalMileage");
        String totalMileageStr = HexUtils.intToHex(totalMileage, 8);
        sb.append(totalMileageStr);
        //
        Integer batteryVoltage = data.getInteger("BatteryVoltage");
//        String format3 = new DecimalFormat("0.0").format(batteryVoltage);
//        String replace3 = format3.replace(".", "");
        String batteryVoltageStr = HexUtils.intToHex(batteryVoltage, 4);
        sb.append(batteryVoltageStr);

        return sb.toString();
    }

    public void setData(String data) {
        this.Data = data;
    }

    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append(this.getDataID());
        sb.append(this.getLength());
        sb.append(this.getVehicleModel(jsonObject));
        sb.append(this.getData(jsonObject));
        return sb.toString();
    }
}
